package com.snap.vseries.pdf;


import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.snap.vseries.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Rachel
 */
public class PdfHeaderAndFooterHelper extends PdfPageEventHelper {
    private final String fileName;
    PdfTemplate totalPage;
    Font hfFont;

    {
        hfFont = FontFactory.getFont(Constants.NormalFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10, Font.BOLD);
    }

    public PdfHeaderAndFooterHelper(String fileName) {
        this.fileName = fileName;
    }

    /**
     * 打开文档时，创建一个总页数的模版
     */
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        totalPage = cb.createTemplate(24, 50);
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            PdfPTable table = new PdfPTable(2);
            table.setTotalWidth(PageSize.A4.getWidth() - 46 * 2);
            table.setWidths(new int[]{24, 24});
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(-10);
            table.getDefaultCell().setPaddingBottom(12);
            table.getDefaultCell().setBorder(Rectangle.BOTTOM);
            table.getDefaultCell().setBorderWidth(1);
            table.addCell(new Paragraph(fileName, hfFont));
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            String time = new SimpleDateFormat("打印时间yyyy/MM/dd HH:mm").format(new Date());
            table.addCell(new Paragraph(time, hfFont));
            table.writeSelectedRows(0, -1, 46, PageSize.A4.getHeight() - 36, writer.getDirectContent());
            if (writer.getPageNumber() != 1) {
                PdfPTable pageTable = new PdfPTable(2);
                pageTable.setTotalWidth(PageSize.A4.getWidth() - 46 * 2);
                pageTable.setWidths(new int[]{24, 24});
                pageTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                pageTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
                pageTable.addCell(new Paragraph("Page  " + String.format("%02d", writer.getPageNumber()) + " / ", hfFont));
                PdfPCell cell = new PdfPCell(Image.getInstance(totalPage));
                cell.setBorder(Rectangle.NO_BORDER);
                pageTable.addCell(cell);
                pageTable.writeSelectedRows(0, -1, 46, 50, writer.getDirectContent());
            }
        } catch (Exception de) {
            throw new ExceptionConverter(de);
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        String text = String.format("%02d", writer.getPageNumber());
        ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT, new Paragraph(text, hfFont), 0, 50 - 12, 0);
    }
}